package com.example.rwq_test.niuke.stack;

import java.util.Arrays;

/**
 * TODO note
 *
 * @author renweiqiang
 * @version 1.0.0
 * @createTime 2023/12/11
 */
public class QuickSort {
    public static void main(String[] args) {
        int[] nums = {9, 8, 7, 4 ,5, 6, 9, 8, 7};
        quickSort(nums);
        System.out.println(Arrays.toString(nums));
    }

    public static void quickSort(int[] arr) {
        quickSort(arr, 0, arr.length - 1);
    }

    public static void quickSort(int[] arr, int start, int end) {
        if (start >= end) {
            return;
        }
        int base = arr[start];
        int left = start;
        int right = end;

        while (left < right) {
            while (arr[right] >= base && left < right) {
                right --;
            }
            while (arr[left] <= base && left < right) {
                left ++;
            }

            if (left < right) {
                int temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
            }
        }

        arr[start] = arr[left];
        arr[left] = base;

        quickSort(arr, start, left - 1);
        quickSort(arr, left + 1, end);
    }
}
